<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require_once APPPATH . 'controllers/admin.php';

/**
 * Контроллер просмотра и редактирования жанров видео
 *
 * @package CoreFramework
 * @version 1.0
 * @author Gennadiy Kozlenko
 */
class Admin_Manage_Genres extends Admin {
   
   /**
    * Конструктор класса
    *
    * @access public
    * @return Admin_Manage_Genres
    */
   function Admin_Manage_Genres() {
      parent::Admin();
      $this->set_menu(6, 2);
      $this->load->language('button');
      $this->load->language('video');
   } // end Admin_Manage_Genres
   
   /**
    * Метод по умолчанию
    *
    * @access public
    */
   function index() {
      $data = array();
      $this->load->helper('link');
      $this->load->model('m_select_genres');
      $this->m_select_genres->add_sort('title');
      $this->load->library('table_builder');
      $this->table_builder->set_model($this->m_select_genres);
      $this->table_builder->add_button(create_link($this->get_controller(), 'add'), $this->lang->line('add_button'));
      $this->table_builder->add_button(create_link('admin_join_genres'), $this->lang->line('join_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'edit'), $this->lang->line('edit_button'));
      $this->table_builder->add_button(create_link($this->get_controller(), 'delete'), $this->lang->line('delete_button'), $this->lang->line('genres_delete_confirmation'));
      $data['table'] = $this->table_builder->generate();
      $this->set_content($this->parser->parse('admin/manage_genres.html', $data, true));
      $this->display();
   } // end index
   
   /**
    * Метод удаления записей
    *
    * @access public
    */
   function delete() {
      $ids = $this->input->post('ids');
      $count = 0;
      if (is_array($ids) && 0 < count($ids)) {
         $this->load->model('m_genre');
         foreach ($ids as $id) {
            $this->m_genre->set_id($id);
            if ($this->m_genre->delete()) {
               $count++;
            }
         }
         if (0 == $count) {
            $this->set_message($this->lang->line('delete_genres_error'), 'error', true);
         } elseif ($count != count($ids)) {
            $this->set_message($this->lang->line('delete_genres_warn'), 'help', true);
         } else {
            $this->set_message($this->lang->line('delete_genres_succ'), 'info', true);
         }
      }
      $this->redirect($this->get_controller());
   } // end delete
   
   /**
    * Метод добавления записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function add() {
      $this->redirect('admin_update_genre');
   } // end add
   
   /**
    * Метод изменения записи
    * Редирект на нужный контроллер
    *
    * @access public
    */
   function edit() {
      $ids = $this->input->post('ids');
      if (is_array($ids) && 0 < count($ids)) {
         $id = array_shift($ids);
         $this->redirect('admin_update_genre', '', array('id' => $id));
      }
      $this->redirect($this->get_controller());
   } // end edit
   
}
